//
//  AIBasicViewController.m
//  aixianfeng
//
//  Created by 李江波 on 2016/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIBasicViewController.h"

#import "AISaoyisaoViewController.h"
#import "AISearchController.h"
#import "AIAddressView.h"

#import "AIAddressViewController.h"
#import "PYSearchViewController.h"
#import "AIMainNavController.h"

@interface AIBasicViewController ()<PYSearchViewControllerDelegate>
@property(nonatomic ,strong) AIAddressView *addressView;
@end

@implementation AIBasicViewController

-(instancetype)init{

    self = [super init];
    
    _addressView = [[AIAddressView alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
    self.navigationItem.titleView = _addressView;
    [_addressView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"changeName" object:nil];
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *saoyisao = [UIButton buttonWithType:0];
    NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:@"icon_black_scancode"] imageWH:23.5 title:@"扫一扫" fontSize:10 titleColor:[UIColor blackColor] spacing:6];
    [saoyisao setAttributedTitle:str forState:UIControlStateNormal];
    [saoyisao sizeToFit];
    saoyisao.titleLabel.numberOfLines = 0;
    saoyisao.titleLabel.textAlignment = NSTextAlignmentCenter;
    [saoyisao.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(saoyisao);
        make.top.equalTo(saoyisao).offset(0);
        make.bottom.equalTo(saoyisao);
    }];
    [saoyisao layoutIfNeeded];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saoyisao];
    
    [saoyisao addTarget:self action:@selector(saoyisao) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *searcher = [UIButton buttonWithType:0];
    NSAttributedString *str1 = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:@"icon_search"] imageWH:23.5 title:@"扫一扫" fontSize:10 titleColor:[UIColor blackColor] spacing:6];
    [searcher setAttributedTitle:str1 forState:UIControlStateNormal];
    [searcher sizeToFit];
    searcher.titleLabel.numberOfLines = 0;
    searcher.titleLabel.textAlignment = NSTextAlignmentCenter;
    [searcher.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(searcher);
        make.top.equalTo(searcher).offset(0);
        make.bottom.equalTo(searcher);
    }];
    [searcher layoutIfNeeded];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:searcher];
    
    [searcher addTarget:self action:@selector(clickSearcher) forControlEvents:UIControlEventTouchUpInside];
//    _addressView = [[AIAddressView alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
//    self.navigationItem.titleView = _addressView;
//    [_addressView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];
//    
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"changeName" object:nil];
}

-(void)saoyisao{

    [self.navigationController pushViewController:[[AISaoyisaoViewController alloc]init] animated:YES];
}

-(void)clickSearcher{
 
    // 1.创建热门搜索
    NSArray *hotSeaches = @[@"bobo", @"BOBO", @"波波", @"波波", @"扫地僧", @"帅哥", @"Mac", @"Swift", @"OC", @"喔喔喔", @"🎈", @"😉"];
    // 2. 创建控制器
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"爱蜂网搜美食" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        // 开始搜索执行以下代码
        // 如：跳转到指定控制器
        [searchViewController.navigationController pushViewController:[[AISearchController alloc] init] animated:YES];
    }];

    searchViewController.hotSearchStyle = PYHotSearchStyleDefault; // 热门搜索风格为默认
    searchViewController.searchHistoryStyle = PYSearchHistoryStyleColorfulTag; // 搜索历史风格根据选择
    // 4. 设置代理
    searchViewController.delegate = self;
    // 5. 跳转到搜索控制器
    AIMainNavController *nav = [[AIMainNavController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];
}
#pragma mark - PYSearchViewControllerDelegate
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
    if (searchText.length) { // 与搜索条件再搜索
        // 根据条件发送查询（这里模拟搜索）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 搜索完毕
            // 显示建议搜索结果
            NSMutableArray *searchSuggestionsM = [NSMutableArray array];
            for (int i = 0; i < arc4random_uniform(5) + 10; i++) {
                NSString *searchSuggestion = [NSString stringWithFormat:@"搜索建议 %d", i];
                [searchSuggestionsM addObject:searchSuggestion];
            }
            // 返回
            searchViewController.searchSuggestions = searchSuggestionsM;
        });
    }
}

-(void)tap{
    
    AIAddressViewController *vc = [[AIAddressViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    [vc setChangeAddressBlock:^(NSString *str) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeName" object:nil userInfo:@{@"user":str}];
//        weakself.addressView.address = str;
    }];
}

-(void)change:(NSNotification *)obj{

    self.addressView.address = obj.userInfo[@"user"];
}

@end
